﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestSystem.DataModel;
using System.IO;

namespace TestSystem
{
    public partial class StudentHomePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (BindingSource.GetInstance().CurrentUser != null)
                {
                    this.WelcomeLabel.Text = String.Format("Welcome : {0}", BindingSource.GetInstance().CurrentUser.UserName);
                }
                else
                {
                    this.WelcomeLabel.Text = "Welcome Guset";
                }

                /*这个地方我们查询了所有的考试，同时默认所有的学生都参加了所有的考试，
                 * 但是对于正确的用户故事，我们应该支持根据用户的id查询该用户参加的考试
                 */

                this.tests.Clear();
                GetTests();

                this.TestList.DataSource = this.tests;

                this.TestList.DataBind();
            }
        }

        private List<Test> tests = new List<Test>();

        private void GetTests()
        {
            String filePath = BindingSource.GetInstance().TestFilePath;

            if (File.Exists(filePath))
            {
                using (StreamReader streamReader = new StreamReader(filePath))
                {
                    String arg = String.Empty;
                    while (true)
                    {
                        arg = streamReader.ReadLine();

                        if (String.IsNullOrEmpty(arg))
                        {
                            break;
                        }

                        String[] args = arg.Split(' ');

                        Test test = new Test(args[0], args[1], args[2], args[3]);

                        this.tests.Add(test);
                    }
                }
            }
        }


        protected void StartTestButton_Click(Object sender, EventArgs e)
        {
            if (this.TestList.SelectedIndex != -1)
            {
                SetTestInformation(this.TestList.SelectedIndex);
            }
            else
            {
                SetTestInformation(0);
            }
        }

        private void SetTestInformation(int testID)
        {

            String filePath = String.Format(@"D:\TestSystem\test{0}.txt", testID);

            if (File.Exists(filePath))
            {
                BindingSource.GetInstance().ChooseList.Clear();
                BindingSource.GetInstance().BlankList.Clear();

                using (StreamReader streamReader = new StreamReader(filePath, System.Text.Encoding.UTF8))
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        BindingSource.GetInstance().ChooseList.Add(
                            new Choose(
                                String.Format("{0}.{1}",(i + 1).ToString(), streamReader.ReadLine())));
                    }

                    for (int i = 0; i < 5; ++i)
                    {
                        BindingSource.GetInstance().BlankList.Add(
                           new Blank(
                                String.Format("{0}.{1}", (i + 1).ToString(), streamReader.ReadLine())));
                    }
                }
            }

            Response.Redirect("TestPage.aspx");
        }


    }
}